package com.cedarhd.brzx.domain.annotation;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.regex.Pattern;

/**
 * 手机号码验证
 * @author Evan
 * @date 2018-05-07
 */
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PhoneXs.Validator.class)
public @interface PhoneXs {
    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    String message() default "invalid input";

    class Validator implements ConstraintValidator<PhoneXs, String> {

        @Override
        public void initialize(PhoneXs notEmptyDemo) {}

        @Override
        public boolean isValid(String arg0, ConstraintValidatorContext context) {

            String pattern = "^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$";

            return Pattern.matches(pattern,arg0);
        }
    }
}
